package com.hopedove.workshop.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.hopedove.workshop.R
import com.hopedove.workshop.data.pojo.ObservableRunningSituation
import com.hopedove.workshop.databinding.WorkshopRunningSituationQueryListItemBinding

class RunningSituationQueryAdapter : RecyclerView.Adapter<RunningSituationQueryAdapter.VH>() {

    private val data = mutableListOf<ObservableRunningSituation>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val binding = DataBindingUtil.inflate<WorkshopRunningSituationQueryListItemBinding>(
            LayoutInflater.from(parent.context),
            R.layout.workshop_running_situation_query_list_item,
            parent,
            false
        )
        return VH(binding)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        holder.render(data, position)
    }

    override fun getItemCount(): Int = data.size

    fun setData(list: List<ObservableRunningSituation>) {
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }

    class VH(val binding: WorkshopRunningSituationQueryListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun render(data: List<ObservableRunningSituation>, position: Int) {
            binding.item = data[position]
        }

    }
}